package shiyl.use.springboot.requestparam.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.AsyncHandlerInterceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static java.lang.String.format;

/**
 * 执行Controller的方法 前/后/执行完时，现实信息的拦截器
 * @author shiyl
 */
@Slf4j
@Component
public class ShowInfoHandlerInterceptor implements HandlerInterceptor{

    /**
     * 执行Controller方法前，现实请求信息
     *
     * @param request
     * @param response
     * @param handler
     * @return true表示继续流程；false表示流程中断，不会继续调用其他的拦截器或处理器
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        log.info("===========> 准备执行");
        log.info(format("request protocol: %s",request.getProtocol()));
        log.info(format("request url     : %s",request.getRequestURI()));
        log.info(format("request method  : %s", request.getMethod()));
        log.info(format("handler class   : %s", handler.getClass().getName()));
        return true;
    }


    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        log.info("===========> 执行后");
    }


    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        log.info("===========> 执行完成");

    }
}
